﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace jQueryAjax
{

    public class Book
    {
        public string Title { get; set; }
        public string Author { get; set; }
        public string Genre { get; set; }
    }

    public class AjaxXML
    {
        /// <summary>
        /// Tạo List để thử
        /// </summary>
        /// <returns></returns>
        public List<Book> cList()
        {
            List<Book> tempList = new List<Book>();

            tempList.Add(new Book { Title = "Alchemist", Author = "Paulo Coelho", Genre = "Fiction" });
            tempList.Add(new Book { Title = "Heart of Darkness", Author = "Joseph Conrad", Genre = "Classic" });

            return tempList;
        }

        /// <summary>
        /// Chuyển từ List => XML string
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public XmlDocument SerializeObject(object obj)
        {
            XmlDocument xmlDoc = new XmlDocument();
            XmlSerializer serializer = new XmlSerializer(obj.GetType());
            using (MemoryStream ms = new MemoryStream())
            {
                serializer.Serialize(ms, obj);
                ms.Position = 0;
                xmlDoc.Load(ms);
                return xmlDoc;
            }
        }
    }
}